window.onload = function() {
    ifLoged();
    //动态加载文章名至页面名称
    var abstract = localStorage.abstract;
    //请求文章
    var datax = window.location.href;
    datax = datax.split("?")
    datax = datax[datax.length - 1];
    var id = datax.split('=')[1];
    if (id != null) {
        //加载文章详情
        ajaxXHR("GET", url + "posts/details?id=" + id, function(data) {
            if (data.code != "SUCCESS") {
                console.log(data.message);
                return false;
            }
            console.log(data);
            dataa = data.data.article;
            console.log(dataa.author.avatar);
            // document.getElementsByClassName("author-icon")[0].src = url_file + dataa.pic;
            document.getElementsByClassName("author-icon")[0].src = "image/3.png";
            document.getElementsByClassName("authorName")[0].innerHTML = dataa.author.name;
            document.getElementsByClassName("publishTime")[0].innerHTML = moment(dataa.create_time).format('YYYY-MM-DD HH:mm:ss');
            document.getElementsByClassName("praisecount")[0].innerHTML = dataa.praise_sum;
            document.getElementsByClassName("visitcount")[0].innerHTML = dataa.look_sum;
            console.log(dataa.pic);
            // document.getElementById("cover").src = url_file + data.pic;
            document.getElementsByClassName("titile")[0].innerHTML = dataa.title;
            document.getElementsByClassName("essayBody")[0].innerHTML = dataa.body;
        })



        ajaxXHR("GET", url + "comment/list?page=1&limit=3&article=" + id, function(data) {
            if (data.code != "SUCCESS" || data.data.comments.length == 0) {
                document.getElementsByClassName("commentListTitle")[0].innerHTML = "无法加载更多评论！";
                console.log(data.message);
                return false;
            }
            // console.log(data);
            commentdata = data.data.comments;
            var comment = "";
            for (let i = 0; i < commentdata.length; i++) {
                // console.log(commentdata.length);
                const e = commentdata[i];
                comment += '<div class="commentUnit"><a href = "homePage.html?id=' + e._id + ' ">';
                preimgsrc = "image/success.jpg";
                comment += '<img class = "user-icon" src = "' + preimgsrc + '"><span class="userName fc000">' + e.author.name + '</span></a>';
                comment += '<span class = "commentTime" >' + moment(e.create_time).format('YY-MM-DD HH:mm:ss') + '</span><a class="commentPCount"><i> </i><span class="commentpcount">' + e.praise_sum + ' </span></a>';
                comment += '</div><p class="commentContent font18"> ' + e.body + '</p>';
                console.log(e.author.avatar);
            }
            document.getElementsByClassName("commentList")[0].insertAdjacentHTML('beforeend', comment);

        })
    }

    // 动态加载评论


    /**
     * 给a需要点击定向跳转的a进行设置
     */
    document.getElementById("homepageA").href = "homepage.html?id=" + id,



        document.getElementById("submitComment").onclick = function() {
            var comment = document.getElementById("commentSubmit").value;
            var parm = 'token=' + localStorage.token + '&body=' + comment + '&article=' + id;
            console.log(parm);
            ajaxXHR("POST", url + "comment/add", function(data) {
                if (data.code != 'SUCCESS') {
                    alert(data.message);
                } else {
                    alert("评论成功！");
                    window.location.href = "articleDetail.html?id=" + id;
                }
            }, 'token=' + localStorage.token + '&body=' + comment + '&article=' + id)
        }
    document.getElementById("logout").onclick = function confirmLogout() {
        var msg = "您真的确定要退出登陆吗？\n\n请确认！";
        if (confirm(msg) == true) {
            localStorage.removeItem("token");
            window.location.href = "register&login.html";
        } else {
            return false;
        }
    }
}